package ezXpns.GUI;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import ezXpns.ManageProfile;
import ezXpns.Profile;

import java.awt.Color;
import java.io.IOException;
/**
 * @author A0086894H
 * This dialog allows users to create an account
 */


//@author A0086894H
public class AccountCreationGUI extends JDialog {
	public AccountCreationGUI() {
	}
	

	/**
	 * constructor: creates the dialog window
	 */
	private boolean createNewProfile = false;
	public Profile showDialog(){
		createNewProfile = true;
		return showDialog(new Profile());
	}
	
	public Profile showDialog(Profile loadProfile){
		JDialog dialog = new JDialog(this, Dialog.ModalityType.APPLICATION_MODAL);	    
		dialog.setBounds(350, 350, 200, 200);	    
	    dialog.getContentPane().setLayout(new FlowLayout());
	    
	    if(loadProfile!=null)profile = loadProfile;
	    else profile = new Profile();
		
		dialog.setBackground(Color.WHITE);
		dialog.setForeground(Color.BLACK);
		dialog.setTitle("Account Creation");
		
	    
		/**
		 * Caret listener for all the necessary fields of account creation
		 */
		CaretListener mandatoryCheck = new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				// to check if budget field is a valid float
				boolean isFloat;
				try{
					Float.parseFloat(txtBudget.getText());
					isFloat=true;
				} catch (NumberFormatException e){
					isFloat=false;
				}
				// checks and sets the enablility of "create" button
				if (!txtUserName.getText().equals("") &&					    // if user name is entered
						!txtBudget.getText().equals("") && 						// if budget field is entered
						isFloat
						){
					createButton.setEnabled(true);
				}
				else {
					createButton.setEnabled(false);				
				}
			}
		};		
		
		// Creates content panel
		dialog.setBounds(100, 100, 450, 300);
		dialog.getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[153.00][grow][]", "[][][][][88.00]"));
		// label: username
		{
			JLabel lblUserName = new JLabel("User Name");
			lblUserName.setFont(new Font("SansSerif", Font.BOLD, 15));
			contentPanel.add(lblUserName, "cell 0 0,alignx right");
		}
		// field: username
		{
			txtUserName = new JTextField();
			txtUserName.addCaretListener(mandatoryCheck);
			txtUserName.setToolTipText("User Name");
			contentPanel.add(txtUserName, "cell 1 0,growx");
			txtUserName.setColumns(10);
		}
		// label: budget
		{
			JLabel lblNewLabel_2 = new JLabel("Monthly Budget $");
			lblNewLabel_2.setFont(new Font("SansSerif", Font.BOLD, 15));
			contentPanel.add(lblNewLabel_2, "cell 0 1,alignx trailing");
		}
		// field: budget
		{
			txtBudget = new JTextField();
			txtBudget.setToolTipText("Set your budget for the month");
			txtBudget.addCaretListener(mandatoryCheck);
			contentPanel.add(txtBudget, "cell 1 1,growx");
			txtBudget.setColumns(10);
		}
		// label: profile picture
		/*{
			JLabel lblProfilePictureoptional = new JLabel("Profile Picture (optional)");
			lblProfilePictureoptional.setFont(new Font("SansSerif", Font.BOLD, 15));
			contentPanel.add(lblProfilePictureoptional, "cell 0 2,alignx trailing");
		}
		// field: profile picture path
		{
			txtPicture = new JTextField();
			txtPicture.setToolTipText("Directory of your picture");
			contentPanel.add(txtPicture, "cell 1 2,growx");
			txtPicture.setColumns(10);
		}
		// TODO store a local copy of resized image
		// button: upload profile picture
		{
			JButton btnBrowse = new JButton("Browse...");
			btnBrowse.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					final JFileChooser fc = new JFileChooser();
					FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG,GIF,PNG Images", "jpg", "jpeg", "gif", "png");
					fc.setFileFilter(filter);
					int returnVal=fc.showOpenDialog(AccountCreationGUI.this);
					if(returnVal == JFileChooser.APPROVE_OPTION){
						txtPicture.setText(fc.getSelectedFile().getAbsolutePath()); // sets directory path into field TODO 
					}
				}
			});
			contentPanel.add(btnBrowse, "cell 2 2");
		}
		// label: picture size recommendations
		{
			JLabel lblpictureRecommendations = new JLabel();
			lblpictureRecommendations.setFont(new Font("Times New Roman", Font.ITALIC, 10)); 
			lblpictureRecommendations.setText("128px by 128px recommended");
			contentPanel.add(lblpictureRecommendations, "cell 1 3");
		}*/
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			dialog.getContentPane().add(buttonPane, BorderLayout.SOUTH);
			// button: create
			{
				createButton = new JButton("Create");
				createButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						
						profile.setUsername(txtUserName.getText());
						profile.setBudget(Float.parseFloat(txtBudget.getText()));
						//profile.setFilepath(txtPicture.getText());
						
						if(createNewProfile){
							try {
								ManageProfile.addUser(profile);
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
						
						dispose();
						
					}
				});
				createButton.setEnabled(false);
				buttonPane.add(createButton);
			}
			// button: cancel
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);

			}
		}
		
	    
	   
	    loadProfile(profile);
	    
	    dialog.setVisible(true);    
	    
	    return profile;

		
	}
	
	public void loadProfile(Profile loadProfile){
		this.profile = loadProfile;
		txtUserName.setText(profile.getUsername());
		txtBudget.setText(Float.toString(profile.getBudget()));
		//txtPicture.setText(profile.getFilepath());
	}
	
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtUserName;
	private JTextField txtBudget;
	private JTextField txtPicture;
	private JButton createButton;
	private Profile profile;
}